package br.com.ozsolucoes.relatorio;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

//import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

public class GenericBean 
{
	
	public GenericBean(){}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static HashMap listarAtributosValores(Object objeto,HashMap paramsEntrada){  
        Class classe = objeto.getClass();  
        Field[] campos = classe.getDeclaredFields();
        Method[] metodos = classe.getDeclaredMethods();
        
        HashMap params = new HashMap();
           
       for (int i = 0; i<campos.length; i++){
        	campos[i].setAccessible(true);
            try {
				
            	params.put(!paramsEntrada.containsKey(campos[i].getName()) ? campos[i].getName(): "", campos[i].get(objeto));
         
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}  
        }
        
       for (int i = 0; i < metodos.length; i++) {
       	Method m = metodos[i];
       	if(m.getName().startsWith("get") && (m.getName().contains("Formatado") || m.getName().contains("Formatada"))){
       		String nome = m.getName().substring(m.getName().indexOf("get")+3);
       		nome = nome.substring(0,1).toLowerCase() + nome.substring(1);
       		try {
				params.put(nome,m.invoke(objeto));
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
       	}
			
		}
        return params;  
    }
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static HashMap adicionaCabecalho(Map entrada, FacesContext context ) {
		
		entrada.put("logo", context.getExternalContext().getRealPath(ConstantesRelatorio.IMAGEM_LOGO));
		
		return (HashMap) entrada;
	}
	
}